﻿using System;
using System.Collections;

namespace ExFlash.Records
{
    public class ClipEventFlags : ISwfSerializable
    {
        private BitArray mBitArray;
        private int mSwfVersion;

        private const int ClipEventKeyUpIndex = 0;
        private const int ClipEventKeyDownIndex = 1;
        private const int ClipEventMouseUpIndex = 2;
        private const int ClipEventMouseDownIndex = 3;
        private const int ClipEventMouseMoveIndex = 4;
        private const int ClipEventUnloadIndex = 5;
        private const int ClipEventEnterFrameIndex = 6;
        private const int ClipEventLoadIndex = 7;
        private const int ClipEventDragOverIndex = 8;
        private const int ClipEventRollOutIndex = 9;
        private const int ClipEventRollOverIndex = 10;
        private const int ClipEventReleaseOutsideIndex = 11;
        private const int ClipEventReleaseIndex = 12;
        private const int ClipEventPressIndex = 13;
        private const int ClipEventInitializeIndex = 14;
        private const int ClipEventDataIndex = 15;
        private const int ClipEventConstructIndex = 21;
        private const int ClipEventKeyPressIndex = 22;
        private const int ClipEventDragOutIndex = 23;
        private const int ArraySize = 24;

        public Boolean ClipEventKeyUp
        {
            get
            {
                return mBitArray[ClipEventKeyUpIndex];
            }
            private set
            {
                mBitArray[ClipEventKeyUpIndex] = value;
            }
        }
        public Boolean ClipEventKeyDown
        {
            get
            {
                return mBitArray[ClipEventKeyDownIndex];
            }
            private set
            {
                mBitArray[ClipEventKeyDownIndex] = value;
            }
        }

        public Boolean ClipEventMouseUp
        {
            get
            {
                return mBitArray[ClipEventMouseUpIndex];
            }
            private set
            {
                mBitArray[ClipEventMouseUpIndex] = value;
            }
        }

        public Boolean ClipEventMouseDown
        {
            get
            {
                return mBitArray[ClipEventMouseDownIndex];
            }
            private set
            {
                mBitArray[ClipEventMouseDownIndex] = value;
            }
        }

        public Boolean ClipEventMouseMove
        {
            get
            {
                return mBitArray[ClipEventMouseMoveIndex];
            }
            private set
            {
                mBitArray[ClipEventMouseMoveIndex] = value;
            }
        }

        public Boolean ClipEventUnload
        {
            get
            {
                return mBitArray[ClipEventUnloadIndex];
            }
            private set
            {
                mBitArray[ClipEventUnloadIndex] = value;
            }
        }

        public Boolean ClipEventEnterFrame
        {
            get
            {
                return mBitArray[ClipEventEnterFrameIndex];
            }
            private set
            {
                mBitArray[ClipEventEnterFrameIndex] = value;
            }
        }

        public Boolean ClipEventLoad
        {
            get
            {
                return mBitArray[ClipEventLoadIndex];
            }
            private set
            {
                mBitArray[ClipEventLoadIndex] = value;
            }
        }

        public Boolean ClipEventDragOver
        {
            get
            {
                return mBitArray[ClipEventDragOverIndex];
            }
            private set
            {
                mBitArray[ClipEventDragOverIndex] = value;
            }
        }

        public Boolean ClipEventRollOut
        {
            get
            {
                return mBitArray[ClipEventRollOutIndex];
            }
            private set
            {
                mBitArray[ClipEventRollOutIndex] = value;
            }
        }

        public Boolean ClipEventRollOver
        {
            get
            {
                return mBitArray[ClipEventRollOverIndex];
            }
            private set
            {
                mBitArray[ClipEventRollOverIndex] = value;
            }
        }

        public Boolean ClipEventReleaseOutside
        {
            get
            {
                return mBitArray[ClipEventReleaseOutsideIndex];
            }
            private set
            {
                mBitArray[ClipEventReleaseOutsideIndex] = value;
            }
        }

        public Boolean ClipEventRelease 
        {
            get
            {
                return mBitArray[ClipEventReleaseIndex];
            }
            private set
            {
                mBitArray[ClipEventReleaseIndex] = value;
            }
        }

        public Boolean ClipEventPress
        {
            get
            {
                return mBitArray[ClipEventPressIndex];
            }
            private set
            {
                mBitArray[ClipEventPressIndex] = value;
            }
        }

        public Boolean ClipEventInitialize
        {
            get
            {
                return mBitArray[ClipEventInitializeIndex];
            }
            private set
            {
                mBitArray[ClipEventInitializeIndex] = value;
            }
        }

        public Boolean ClipEventData
        {
            get
            {
                return mBitArray[ClipEventDataIndex];
            }
            private set
            {
                mBitArray[ClipEventDataIndex] = value;
            }
        }

        public Boolean ClipEventConstruct
        {
            get
            {
                return mBitArray[ClipEventConstructIndex];
            }
            private set
            {
                mBitArray[ClipEventConstructIndex] = value;
            }
        }

        public Boolean ClipEventKeyPress
        {
            get
            {
                return mBitArray[ClipEventKeyPressIndex];
            }
            private set
            {
                mBitArray[ClipEventKeyPressIndex] = value;
            }
        }

        public Boolean ClipEventDragOut
        {
            get
            {
                return mBitArray[ClipEventDragOutIndex];
            }
            private set
            {
                mBitArray[ClipEventDragOutIndex] = value;
            }
        }

        public ClipEventFlags(int Version)
        {
            mBitArray = new BitArray(ArraySize);
            mSwfVersion = Version;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            ClipEventKeyUp = reader.ReadBoolean();
            ClipEventKeyDown = reader.ReadBoolean();
            ClipEventMouseUp = reader.ReadBoolean();
            ClipEventMouseDown = reader.ReadBoolean();
            ClipEventMouseMove = reader.ReadBoolean();
            ClipEventUnload = reader.ReadBoolean();
            ClipEventEnterFrame = reader.ReadBoolean();
            ClipEventLoad = reader.ReadBoolean();

            if (mSwfVersion >= 6)
            {
                ClipEventDragOver = reader.ReadBoolean();
                ClipEventRollOut = reader.ReadBoolean();
                ClipEventRollOver = reader.ReadBoolean();
                ClipEventReleaseOutside = reader.ReadBoolean();
                ClipEventRelease = reader.ReadBoolean();
                ClipEventPress = reader.ReadBoolean();
                ClipEventInitialize = reader.ReadBoolean();
            }

            ClipEventData = reader.ReadBoolean();

            if (mSwfVersion >= 6)
            {
                reader.SkipBits(5);

                if (mSwfVersion >= 7)
                {
                    ClipEventConstruct = reader.ReadBoolean();
                }
                else
                {
                    reader.SkipBits(1);
                }

                ClipEventKeyPress = reader.ReadBoolean();
                ClipEventDragOut = reader.ReadBoolean();

                // Reserved
                reader.SkipBits(8);
            }

            reader.AlignBits();
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
